package com.person.mengshuai.mytestdemo.exception;

import com.person.mengshuai.mytestdemo.domain.response.Result;
import com.person.mengshuai.mytestdemo.domain.response.ResultCode;
import com.person.mengshuai.mytestdemo.exception.base.BusinessException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 全局异常捕获处理
 *
 * @author 孟帅
 * @since 2024/1/10
 */
@ControllerAdvice
@Slf4j
public class GlobalException {
    @ExceptionHandler(HttpRequestMethodNotSupportedException.class)
    @ResponseBody
    public Result handleException(HttpRequestMethodNotSupportedException e){
        log.warn("GlobalException handleException",e);
        return Result.failure(ResultCode.METHOD_NOT_ALLOWED.getMessage());
    }

    @ExceptionHandler(BusinessException.class)
    @ResponseBody
    public Result businessException(BusinessException e){
        log.warn("GlobalException businessException",e);
        return Result.failure(e.getMessage());
    }

    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Result exception(Exception e){
        log.warn("GlobalException exception",e);
        return Result.failure(e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    @ResponseBody
    public Result throwable(Throwable e){
        log.warn("GlobalException throwable",e);
        return Result.failure(e.getMessage());
    }


}
